package com.example.ysdt.controller;

import com.example.ysdt.Vo.CategoryVo;
import com.example.ysdt.entiey.Category;
import com.example.ysdt.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Category")
@Api(value = "类别接口", tags = {"类别接口"})
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @PostMapping("queryCategory")
    @ApiOperation("查询类别")
    public Map<String,Object> queryCategory(@RequestBody CategoryVo categoryVo){
        Map<String, Object> Result = new HashMap<>();
        try {
            List<Category> categories = categoryService.queryCategory(categoryVo);
            Result.put("code",0);
            Result.put("message","查询成功");
            Result.put("data", categories);
        }catch (Exception e){
            Result.put("code",-1);
            Result.put("message","查询失败"+e.getMessage());
        }
        return Result;
    }

    @PostMapping("createCategory")
    @ApiOperation("创建类别")
    public Map<String,Object> createCategory(@RequestBody Category category){
        Map<String,Object> Result = new HashMap<>();
        try {
            categoryService.createCategory(category);
            Result.put("code",0);
            Result.put("message","创建成功");
            Result.put("data",category);
        }catch (Exception e){
            Result.put("code",-1);
            Result.put("message","创建失败:"+e.getMessage());
        }
        return Result;
    }

    @PostMapping("deleteCategory")
    @ApiOperation("删除类别")
    public Map<String,Object> deleteCategory(@RequestBody CategoryVo categoryVo){
        Map<String,Object> Result = new HashMap<>();
        try {
            categoryService.deleteCategory(categoryVo);
            List<Category> categories = categoryService.queryCategory(new CategoryVo());
            Result.put("code",0);
            Result.put("message","删除成功");
            Result.put("data", categories);
        }catch (Exception e){
            Result.put("code",-1);
            Result.put("message","删除失败，"+e.getMessage());
        }
        return Result;
    }

}
